// TPFrame.cpp : implementation of the CTPFrame class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"

#include "tpaboutdlg.h"
#include "TPView.h"
#include "TPFrame.h"

#include "TPPlayer.h"
#include "TPDialogX.h"

#include "TPMediaDlg.h"

#define TIMER_UPDATE	1
#define TIME_UPDATE		500

BOOL CTPFrame::PreTranslateMessage(MSG* pMsg)
{
	if (CFrameWindowImpl<CTPFrame>::PreTranslateMessage(pMsg))
	{
		return TRUE;
	}

	return m_view.IsWindow() ? m_view.PreTranslateMessage(pMsg) : FALSE;
}

bool CTPFrame::AppHibernate(bool bHibernate)
{
	// Insert your code here or delete member if not relevant
	return bHibernate;
}

bool CTPFrame::AppNewInstance(LPCTSTR /*lpstrCmdLine*/)
{
	// Insert your code here or delete member if not relevant
	return false;
}

void CTPFrame::AppSave()
{
	CAppInfo info;
	bool bStatus = (UIGetState(ID_VIEW_STATUS_BAR) & UPDUI_CHECKED) == UPDUI_CHECKED;
	info.Save(bStatus, L"Status");
	// Insert your code here
}

BOOL CTPFrame::OnIdle()
{
	UIUpdateToolBar();
	UIUpdateStatusBar();

	return FALSE;
}

LRESULT CTPFrame::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	CAppInfo info;

	CreateSimpleCEMenuBar();
	UIAddToolBar(m_hWndCECommandBar);

	// StatusBar state restoration
	bool bVisible = false;
	//info.Restore(bVisible, L"Status");
	DWORD dwStyle = WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
	if (bVisible)
	{
		dwStyle |= WS_VISIBLE;
	}
	// StatusBar creation
	CreateSimpleStatusBar(ATL_IDS_IDLEMESSAGE, dwStyle);
	UIAddStatusBar(m_hWndStatusBar);
	UISetCheck(ID_VIEW_STATUS_BAR, bVisible);

	m_hWndClient = m_view.Create(m_hWnd);

	// register object for message filtering and idle updates
	CMessageLoop* pLoop = _Module.GetMessageLoop();
	ATLASSERT(pLoop != NULL);
	pLoop->AddMessageFilter(this);
	pLoop->AddIdleHandler(this);

	SetTimer(TIMER_UPDATE, TIME_UPDATE);

	return 0;
}

LRESULT CTPFrame::OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CTPPlayer::Stop();
	PostMessage(WM_CLOSE);

	return 0;
}

LRESULT CTPFrame::OnAction(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	int iCurItem = m_view.GetCurTrack();

	if ( -1 != iCurItem )
	{
		CTPPlayer::Seek(iCurItem);
	}

	return 0;
}

LRESULT CTPFrame::OnViewStatusBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	BOOL bVisible = !::IsWindowVisible(m_hWndStatusBar);
	::ShowWindow(m_hWndStatusBar, bVisible ? SW_SHOWNOACTIVATE : SW_HIDE);
	UISetCheck(ID_VIEW_STATUS_BAR, bVisible);
	UpdateLayout();

	return 0;
}

LRESULT CTPFrame::OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CTPAboutDlg dlg;
	dlg.DoModal();

	return 0;
}


LRESULT CTPFrame::OnTimer(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	UINT uiTimer = wParam;

	switch ( uiTimer )
	{
	case TIMER_UPDATE:
		{
			m_view.UpdateTime();
		}
		break;

	default:
		break;
	}

	return 0;
}

LRESULT CTPFrame::OnFileOpen(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CString strList(_T("\\"));
	CTPFileDialog dlgFile;

	if ( IDOK == CTPFileDialog().DoModal(m_hWnd, (LPARAM)(&strList)) )
	{
		CString strFile;

		int iOld = 0, iFind = strList.Find(_T('|'));

		while ( -1 != iFind )
		{
			strFile = strList.Mid(iOld, iFind - iOld);
			CTPPlayer::LoadTPFile(strFile);

			iOld	= iFind + 1;
			iFind	= strList.Find(_T('|'), iOld);
		}

		if ( 0 < CTPPlayer::GetTPFileCount() )
		{
			m_view.UpdateList();

			CTPPlayer::Seek();
		}
	}

	return 0;
}

LRESULT CTPFrame::OnFileList(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CTPMediaDlg dlg;

	if ( IDOK == dlg.DoModal() )
	{
		int iIndex = dlg.GetSelectIndex();

		if ( -1 != iIndex )
		{
			CTPPlayer::Seek(0, iIndex);
		}
	}

	return 0;
}

LRESULT CTPFrame::OnPlayNext(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CTPPlayer::Next();

	return 0;
}

LRESULT CTPFrame::OnPlayPlay(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CTPPlayer::Play();

	return 0;
}

LRESULT CTPFrame::OnPlayPrev(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CTPPlayer::Prev();

	return 0;
}

LRESULT CTPFrame::OnPlayPause(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CTPPlayer::Pause();

	return 0;
}
